package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.po.MarketCart;
import com.cskaoyan.bean.po.MarketUser;
import com.cskaoyan.bean.vo.wx.CartIndexVo;
import com.cskaoyan.bean.vo.wx.WxCartAddBo;
import com.cskaoyan.bean.vo.wx.WxCartCheckedBo;
import com.cskaoyan.bean.vo.wx.WxCartCheckoutVo;
import com.cskaoyan.service.wx.WxCartService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("wx/cart/")
public class WxCart {

    @Autowired
    WxCartService wxCartService;

    /**
     * 购物车下单
     *
     * @param cartId
     * @param addressId
     * @param couponId
     * @param userCouponId
     * @param grouponRulesId
     */
    @RequestMapping("checkout")
    public BaseRespVo<WxCartCheckoutVo> checkout(@RequestParam("cartId") Integer cartId,
                                                 @RequestParam("addressId") Integer addressId,
                                                 @RequestParam("couponId") Integer couponId,
                                                 @RequestParam("userCouponId") Integer userCouponId,
                                                 @RequestParam("grouponRulesId") Integer grouponRulesId){
        //获取 登录用户信息
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Short goodsNum = (Short) session.getAttribute("goodsNum");
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        WxCartCheckoutVo wxCartCheckoutVo = wxCartService.checkout(primaryPrincipal.getId(), cartId, addressId, couponId, userCouponId, grouponRulesId, goodsNum);
        BaseRespVo<WxCartCheckoutVo> baseRespVo = new BaseRespVo<>();
        baseRespVo.setData(wxCartCheckoutVo);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        return baseRespVo;
    }


    /**
     * 删除选择
     */
    @PostMapping("delete")
    public BaseRespVo<CartIndexVo> delete(@RequestBody WxCartCheckedBo wxCartCheckedBo) {
        //获取 登录用户信息
        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer count = wxCartService.delete(primaryPrincipal.getId(),wxCartCheckedBo);
        //请求{productIds: [234], isChecked: 1}
        CartIndexVo cartIndexVo = wxCartService.index(primaryPrincipal.getId());
        BaseRespVo<CartIndexVo> baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        baseRespVo.setData(cartIndexVo);
        return baseRespVo;
    }


    @RequestMapping("goodscount")
    public BaseRespVo<Integer> goodscount() {
        BaseRespVo<Integer> baseRespVo= new BaseRespVo<>();
        try {
            Subject subject = SecurityUtils.getSubject();
            //获取 登录用户信息
            MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
            Integer count = wxCartService.goodscount(primaryPrincipal.getId());
            baseRespVo.setData(count);

        } catch (Exception e) {
            e.printStackTrace();
            baseRespVo.setData(0);
        }
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        return baseRespVo;
    }


    /**
     * 编辑购物车
     */
    @PostMapping("update")
    public BaseRespVo<CartIndexVo> update(@RequestBody MarketCart marketCart) {
        wxCartService.update(marketCart);
        //请求{productId: 33, goodsId: 1023003, number: 1, id: 72}
        BaseRespVo<CartIndexVo> baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        return baseRespVo;
    }


    /**
     * 修改选中
     */
    @PostMapping("checked")
    public BaseRespVo<CartIndexVo> checked(@RequestBody WxCartCheckedBo wxCartCheckedBo) {
        Subject subject = SecurityUtils.getSubject();
        // 获取 登录用户信息
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        wxCartService.checked(wxCartCheckedBo);
        //请求{productIds: [234], isChecked: 1}
        CartIndexVo cartIndexVo = wxCartService.index(primaryPrincipal.getId());
        BaseRespVo<CartIndexVo> baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        baseRespVo.setData(cartIndexVo);
        return baseRespVo;
    }


    /**
     * 立即购买
     */

    @PostMapping("fastadd")
    public BaseRespVo<Integer> fastadd(@RequestBody WxCartAddBo wxCartAddBo) {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        session.setAttribute("goodsNum",wxCartAddBo.getNumber());
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer i = wxCartService.addCart(primaryPrincipal.getId(), wxCartAddBo);
        BaseRespVo<Integer> baseRespVo = new BaseRespVo<>();
        baseRespVo.setData(i);
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        return baseRespVo;
    }


    /**
     * 添加购物车
     */
    @PostMapping("add")
    public BaseRespVo<Integer> add(@RequestBody WxCartAddBo wxCartAddBo) {
        Subject subject = SecurityUtils.getSubject();
        // 获取 登录用户信息
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        int i = wxCartService.addCart(primaryPrincipal.getId(), wxCartAddBo);
        BaseRespVo<Integer> baseRespVo = new BaseRespVo<>();
        baseRespVo.setData(i);
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        return baseRespVo;
    }

    /**
     * 展示购物车信息
     */
    @RequestMapping("index")
    public BaseRespVo<CartIndexVo> index() {
        //{"errno":0,"data":{"cartTotal":{"goodsCount":29,"checkedGoodsCount":3,"goodsAmount":49892.00,"checkedGoodsAmount":160.00},"cartList":[{"id":23,"userId":1,"goodsId":1181012,"goodsSn":"1234566","goodsName":"亏贼测试","productId":258,"price":2599.00,"number":1,"specifications":["标准"],"checked":false,"picUrl":"http://182.92.235.201:8083/wx/storage/fetch/q2jfoqg9zgtx8w6i7xhl.jpg","addTime":"2022-11-22 11:06:30","updateTime":"2022-11-22 19:14:43","deleted":false},{"id":25,"userId":1,"goodsId":1110016,"goodsSn":"1110016","goodsName":"天然硅胶宠物除毛按摩刷","productId":150,"price":39550.00,"number":1,"specifications":["标准"],"checked":false,"picUrl":"http://yanxuan.nosdn.127.net/3bd73b7279a83d1cbb50c0e45778e6d6.png","addTime":"2022-11-22 11:26:22","updateTime":"2022-11-22 19:11:01","deleted":false},{"id":26,"userId":1,"goodsId":1064021,"goodsSn":"1064021","goodsName":"清欢日式可调节台灯","productId":78,"price":199.00,"number":2,"specifications":["标准"],"checked":false,"picUrl":"http://yanxuan.nosdn.127.net/c83a3881704094ddd3970099ca77d115.png","addTime":"2022-11-22 11:28:37","updateTime":"2022-11-22 19:11:01","deleted":false},{"id":27,"userId":1,"goodsId":1128002,"goodsSn":"1128002","goodsName":"清新趣粉系列居家地毯 青粉拼接","productId":184,"price":599.00,"number":2,"specifications":["标准"],"checked":false,"picUrl":"http://yanxuan.nosdn.127.net/a1094a808ffb3a52a6cb13565a283d98.png","addTime":"2022-11-22 11:39:10","updateTime":"2022-11-22 19:11:01","deleted":false},{"id":28,"userId":1,"goodsId":1181000,"goodsSn":"1181000","goodsName":"母亲节礼物-舒适安睡组合","productId":2,"price":1500.00,"number":2,"specifications":["1.5m床垫*1+枕头*2","玛瑙红"],"checked":false,"picUrl":"quality=90&thumbnail=200x200&imageView","addTime":"2022-11-22 11:44:40","updateTime":"2022-11-22 19:11:01","deleted":false},{"id":32,"userId":1,"goodsId":1152009,"goodsSn":"1152009","goodsName":"魔兽世界 联盟 护腕 一只","productId":233,"price":29.00,"number":1,"specifications":["标准"],"checked":false,"picUrl":"http://yanxuan.nosdn.127.net/ae6d41117717387b82dcaf1dfce0cd97.png","addTime":"2022-11-22 14:26:24","updateTime":"2022-11-22 19:29:11","deleted":false},{"id":33,"userId":1,"goodsId":1151012,"goodsSn":"1151012","goodsName":"简约知性全棉四件套 素雅灰","productId":229,"price":359.00,"number":1,"specifications":["标准"],"checked":false,"picUrl":"http://yanxuan.nosdn.127.net/cb65635dbcef42b68ba21433f4948f5a.png","addTime":"2022-11-22 14:31:34","updateTime":"2022-11-22 19:11:01","deleted":false},{"id":36,"userId":1,"goodsId":1021010,"goodsSn":"1021010","goodsName":"色织水洗棉纯色四件套","productId":30,"price":299.00,"number":1,"specifications":["标准"],"checked":false,"picUrl":"http://yanxuan.nosdn.127.net/25d734cc0b2eae8f63f9deb1e4ad5f64.png","addTime":"2022-11-22 15:07:37","updateTime":"2022-11-22 19:11:01","deleted":false},{"id":37,"userId":1,"goodsId":1045000,"goodsSn":"1045000","goodsName":"绿茶蛋黄酥 200克/4枚入","productId":58,"price":28.00,"number":1,"specifications":["标准"],"checked":false,"picUrl":"http://yanxuan.nosdn.127.net/b2adc3fd9b84a289a1be03e8ee400e61.png","addTime":"2022-11-22 15:10:27","updateTime":"2022-11-22 19:11:01","deleted":false},{"id":38,"userId":1,"goodsId":1152031,"goodsSn":"1152031","goodsName":"魔兽世界-伊利丹颈枕眼罩套装","productId":234,"price":99.00,"number":1,"specifications":["标准"],"checked":false,"picUrl":"http://yanxuan.nosdn.127.net/fd6e78a397bd9e9804116a36f0270b0a.png","addTime":"2022-11-22 15:23:08","updateTime":"2022-11-22 19:11:01","deleted":false},{"id":39,"userId":1,"goodsId":1006002,"goodsSn":"1006002","goodsName":"轻奢纯棉刺绣水洗四件套","productId":7,"price":899.00,"number":1,"specifications":["标准"],"checked":false,"picUrl":"http://yanxuan.nosdn.127.net/8ab2d3287af0cefa2cc539e40600621d.png","addTime":"2022-11-22 15:29:18","updateTime":"2022-11-22 19:11:01","deleted":false},{"id":41,"userId":1,"goodsId":1057036,"goodsSn":"1057036","goodsName":"日式纯色水洗亚麻抱枕","productId":71,"price":79.00,"number":5,"specifications":["标准"],"checked":false,"picUrl":"http://yanxuan.nosdn.127.net/8a9ee5ba08929cc9e40b973607d2f633.png","addTime":"2022-11-22 16:13:24","updateTime":"2022-11-22 19:11:01","deleted":false},{"id":43,"userId":1,"goodsId":1055016,"goodsSn":"1055016","goodsName":"日式纯棉针织条纹抱枕","productId":68,"price":59.00,"number":3,"specifications":["标准"],"checked":false,"picUrl":"http://yanxuan.nosdn.127.net/23e0203f1512f33e605f61c28fa03d2d.png","addTime":"2022-11-22 16:33:52","updateTime":"2022-11-22 19:11:01","deleted":false},{"id":44,"userId":1,"goodsId":1181013,"goodsSn":"11111111","goodsName":"红茶","productId":260,"price":221.00,"number":3,"specifications":["规格值2"],"checked":false,"picUrl":"http://182.92.235.201:8083/wx/storage/fetch/7iu95so66d2takvsvp2h.png","addTime":"2022-11-22 17:01:01","updateTime":"2022-11-22 19:25:50","deleted":false},{"id":45,"userId":1,"goodsId":1181019,"goodsSn":"3333333","goodsName":"商品名称123","productId":268,"price":55.00,"number":2,"specifications":["标准"],"checked":true,"picUrl":"http://182.92.235.201:8083/wx/storage/fetch/9cdozq5s920q45bgjio9.jpg","addTime":"2022-11-22 19:13:33","updateTime":"2022-11-22 19:33:12","deleted":false},{"id":46,"userId":1,"goodsId":1130037,"goodsSn":"1130037","goodsName":"帆布丝羽绒多用坐垫","productId":189,"price":39.00,"number":1,"specifications":["标准"],"checked":false,"picUrl":"http://yanxuan.nosdn.127.net/19ecd7c6f6f31219cf75117238d95139.png","addTime":"2022-11-22 19:27:59","updateTime":"2022-11-22 19:32:52","deleted":false},{"id":47,"userId":1,"goodsId":1181037,"goodsSn":"10086","goodsName":"测试添加啊","productId":286,"price":50.00,"number":1,"specifications":["1G","1T"],"checked":true,"picUrl":"http://182.92.235.201:8083/wx/storage/fetch/l02h89qabszvnpndi53l.jpg","addTime":"2022-11-22 19:32:35","updateTime":"2022-11-22 19:32:35","deleted":false}]},"errmsg":"成功"}
        Subject subject = SecurityUtils.getSubject();
        // 获取 登录用户信息
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        CartIndexVo cartIndexVo = wxCartService.index(primaryPrincipal.getId());
        BaseRespVo<CartIndexVo> baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        baseRespVo.setData(cartIndexVo);
        return baseRespVo;
    }


}
